Add package-specific exception hierarchy - #10
Merged
Merged
Conversation
Introduce a Trm42\CacheDecorator\Exceptions namespace with an abstract CacheDecoratorException base so consumers can catch every package error with a single catch block. The two existing throw sites now use: - MissingDecoratedObjectException (was LogicException) in initDecorated() - UndefinedMethodException (was BadMethodCallException) for undefined forwarded calls, via an override of the ForwardsCalls trait's throwBadMethodCallException(); the message is preserved verbatim. Update tests and README accordingly. This is a breaking change: the thrown types are no longer the SPL LogicException / BadMethodCallException. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Introduces a small, package-owned exception hierarchy under the new
Trm42\CacheDecorator\Exceptionsnamespace so consumers can catch every cache-decorator-specific failure with a singlecatchblock, while still distinguishing the two concrete failure modes.CacheDecoratorException—abstract class … extends \Exception. Base type for every package error; callers catch this.MissingDecoratedObjectException extends CacheDecoratorException— replaces theLogicExceptionininitDecorated()(no instance passed anddecoratedClass()returnednull).UndefinedMethodException extends CacheDecoratorException— replaces theBadMethodCallExceptionraised for a forwarded call to a method that exists nowhere on the decorated object.Why
The package previously threw raw SPL exceptions (
LogicException,BadMethodCallException), which made it impossible to catch cache-decorator-specific errors as a group and coupled error handling to generic classes any dependency might also throw.How
initDecorated()now throwsMissingDecoratedObjectException(message unchanged).UndefinedMethodException. Since calls are forwarded through Laravel'sForwardsCallstrait, this is done by overriding the trait'sthrowBadMethodCallException()helper — the single point that raisedBadMethodCallException— keeping the message (Call to undefined method {Decorator}::{method}()) verbatim so only the thrown type changes.src/Exceptions/(no composer change needed).catch (CacheDecoratorException $e)snippet.Breaking change
The thrown types now extend
\ExceptionviaCacheDecoratorExceptionand are not instances ofLogicException/BadMethodCallException. Any downstreamcatchrelying on those SPL types must be updated. Worth a release-notes/CHANGELOG line.Verification
composer checkgreen: 31 tests pass, PHPStan reports no errors, Pint passes.🤖 Generated with Claude Code